<script setup lang="ts">
import { ref, onMounted } from 'vue';
import countup from '@/components/countup.vue';
import VChart from 'vue-echarts';
import { dashOpt1, dashOpt2, mapOptions } from './chart/options';
import { activities, ranks } from '@/datas/dashboard';
import { use, registerMap } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  VisualMapComponent,
} from 'echarts/components';
import { BarChart, LineChart, PieChart, MapChart } from 'echarts/charts';
import chinaMap from '@/utils/china';
use([
  CanvasRenderer,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  VisualMapComponent,
  BarChart,
  LineChart,
  PieChart,
  MapChart,
]);
registerMap('china', chinaMap);

// 直接使用原始数据，因为我们要显示完整的值
const rankData = ref(ranks);
</script>

<template>
  <div>
    <el-row :gutter="20" class="mgb20">
      <el-col :span="6">
        <el-card shadow="hover" body-class="card-body clickable-card">
          <el-icon class="card-icon bg1">
            <User />
          </el-icon>
          <div class="card-content">
            <countup class="card-num color1" :end="6666" />
            <div>用户访问数量</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" body-class="card-body clickable-card">
          <el-icon class="card-icon bg2">
            <ChatDotRound />
          </el-icon>
          <div class="card-content">
            <countup class="card-num color2" :end="168" />
            <div>系统消息</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" body-class="card-body clickable-card">
          <el-icon class="card-icon bg3">
            <Goods />
          </el-icon>
          <div class="card-content">
            <countup class="card-num color1" :end="8888" />
            <div>商品数量</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" body-class="card-body clickable-card">
          <el-icon class="card-icon bg4">
            <ShoppingCartFull />
          </el-icon>
          <div class="card-content">
            <countup class="card-num color4" :end="568" />
            <div>今日订单量</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="20" class="mgb20">
      <el-col :span="18">
        <el-card>
          <div class="card-header">
            <p class="card-header-title">订单状态</p>
            <p class="card-header-desc">最近一周订单状态,包括订单成交量和订单退货量</p>
          </div>
          <v-chart class="chart" :option="dashOpt1" />
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div class="card-header">
            <p class="card-header-title">品类分布</p>
            <p class="card-header-desc">最近一个月销售商品的品类情况</p>
          </div>
          <v-chart class="chart" :option="dashOpt2" />
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="7">
        <el-card shadow="hover" :body-style="{ height: '400px' }">
          <div class="card-header">
            <p class="card-header-title">时间线</p>
            <p class="card-header-desc">最新的销售动态和活动信息</p>
          </div>
          <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :color="activity.color"
              class="clickable-item"
            >
              <div class="timeline-item">
                <div>
                  <p>{{ activity.content }}</p>
                  <p class="timeline-desc">{{ activity.description }}</p>
                </div>
              </div>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
      <el-col :span="10">
        <el-card shadow="hover" :body-style="{ height: '400px' }">
          <div class="card-header">
            <p class="card-header-title">渠道统计</p>
            <p class="card-header-desc">最近一个月的订单来源统计</p>
          </div>
          <v-chart class="map-chart" :option="mapOptions" />
        </el-card>
      </el-col>
      <el-col :span="7">
        <el-card shadow="hover" :body-style="{ height: '400px' }">
          <div class="card-header">
            <p class="card-header-title">排行榜</p>
            <p class="card-header-desc">销售商品的热门榜单Top5</p>
          </div>
          <div>
            <div class="rank-item clickable-item" v-for="(rank, index) in rankData" :key="index">
              <div class="rank-item-avatar">{{ index + 1 }}</div>
              <div class="rank-item-content">
                <div class="rank-item-top">
                  <div class="rank-item-title">{{ rank.title }}</div>
                  <div class="rank-item-desc">销量: {{ rank.value }}</div>
                </div>
                <el-progress
                  :percentage="rank.percent"
                  :show-text="false"
                  striped
                  :stroke-width="10"
                  :color="rank.color"
                />
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss">
.card-body {
  display: flex;
  align-items: center;
  height: 100px;
  padding: 0;
}

/* 将 clickable-card 样式移到全局样式中，确保 body-class 能正确应用 */
.card-body.clickable-card {
  cursor: pointer;
}
</style>

<style lang="scss" scoped>
.card-content {
  flex: 1;
  text-align: center;
  font-size: 14px;
  color: #999999;
  padding: 0 20px;
}
.card-num {
  font-size: 30px;
}
.card-icon {
  font-size: 50px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  color: #fff;
}
.bg1 {
  background: #2d8cf0;
}

.bg2 {
  background: #64d572;
}

.bg3 {
  background: #f25e43;
}

.bg4 {
  background: #e9a745;
}
.color1 {
  color: #2d8cf0;
}

.color2 {
  color: #64d572;
}

.color3 {
  color: #f25e43;
}

.color4 {
  color: #e9a745;
}
.chart {
  width: 100%;
  height: 400px;
}
.card-header {
  padding-left: 10px;
  margin-bottom: 20px;
}
.card-header-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 5px;
}
.card-header-desc {
  font-size: 14px;
  color: #999;
}
.timeline-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  color: #000;
}
.timeline-time,
.timeline-desc {
  font-size: 12px;
  color: #787878;
}
.rank-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.rank-item-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f2f2f2;
  text-align: center;
  line-height: 40px;
  margin-right: 10px;
}
.rank-item-content {
  flex: 1;
}

.rank-item-desc {
  font-size: 14px;
  color: #999;
}
.map-chart {
  width: 100%;
  height: 350px;
}
.rank-item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #343434;
  margin-bottom: 10px;
}

.clickable-item {
  cursor: pointer;
}
</style>
